/*
**Processapatternstringbycopyingdigitsfromadigitstringintoit,ala
**theIBM360"edit"instruction.
*/
#include <stdio.h>
#include <string.h>

#define NULL 0
#define NUL '\0'
#define DIGIT_SELECTOR '#'
#define SIGNIFICANCE_START '!'
#define TRUE 1
#define FALSE 0
#define reg register

char *edit(reg char *pattern, reg char *digits)
{
    reg int digit;
    reg int pat_char;
    reg int fill;
    reg int significance;
    reg char *first_digit;
    /*
    **Checkformissingdata,andgetfillcharacter.
    */
    if (pattern == NULL || digits == NULL || (fill = *pattern) == '\0')
        return NULL;
    first_digit = NULL;
    significance = FALSE;
    /*
    **Processpatternstringonebyone.
    */
    while ((pat_char = *++pattern) != NUL)
    {
        /*
        **Seewhatmeaningthepatterncharacterhas.
        */
        switch (pat_char)
        {
        caseDIGIT_SELECTOR:
        caseSIGNIFICANCE_START:
            if ((digit = *digits++) == NUL)
            {
                *pattern = NUL;
                return first_digit;
            }
            if (digit == ' ')
                digit = '0';

            if (digit != '0' || pat_char == SIGNIFICANCE_START)
            {
                if (!significance)
                    first_digit = pattern;
                significance = TRUE;
            }
            break;
        default:
            digit = pat_char;
            break;
        }
        /*
         ** Store the proper character in the result.
         */
        *pattern = significance ? digit : fill;
    }
    return first_digit;
}
